/* ****************************************************************************/
/*xxporcamountrp.p -  - qrf                                                  */
/*copyright ccwin-in inc all rights reserved. this is an unpublished work.    */
/*v8:convertmode=report                                                       */
/*v8:webenabled=yes                                                           */
/*v8:runmode=character,windows                                                */
/******************************************************************************/
/* procedure name        : xxporcamountrp .p                                  */
/* procedure type        : qrf report                                         */
/* description           :                                                    */
/* include files         :                                                    */
/* called by             :                                                    */
/* called procedures     :                                                    */
/* project               : Yangchuan ee2022                                   */
/*******************old revisions before cfaa ee migration*********************/
/* created by  GAOYAN      :  date:  8-Nov-24 #                              */
/******************************************************************************/
/*srr:                                                                        */
/******************************************************************************/

{{&us_bbi}mfdeclre.i}
{{&us_bbi}gplabel.i}



{com/qad/shell/report/dsReportRequest.i}
{com/qad/shell/report/ttReportParameter.i} 
{com/qad/shell/report/ReportConstants.i}



/*============================================================================*/
/* ****************************  VARIABLES ******************************* */
/*============================================================================*/

define input parameter runReport    as logical no-undo.
define input parameter reportHandle as handle  no-undo.
define input parameter dataset      for dsReportRequest.
define output parameter dataset-handle phReportResults.

DEFINE VARIABLE vhds          as   handle     NO-UNDO.

/* for searchconditions */

define variable Valuelist           as character no-undo.
define variable Valuelist1          as character no-undo.
define variable vc_supplier1          as character no-undo.
define variable vc_supplier2          as character no-undo.
define variable vc_year          as integer no-undo.
define variable vc_period1          as integer no-undo.
define variable vc_period2          as integer no-undo.
define variable vc_site1         as character no-undo.
define variable vc_site2         as character no-undo.
define variable lc_dt1         as date no-undo.
define variable lc_dt2         as date no-undo.







/*============================================================================*/
/* ****************************  TEMP TABLES  ******************************* */
/*============================================================================*/


/* TEMP-TABLE DEFINITION BLOCK */
/* Search condition            */
   
define temp-table ttSearchCondition no-undo
    field tt_site               as character
    field tt_supplier               as character
    field tt_year               as integer
    field tt_period              as integer

     . 


/* output temp-table */     
define temp-table tt_output  no-undo
 

fields tt_supplier           as   character
fields tt_supplierdesc           as   character
fields tt_year           as   integer
fields tt_period           as   integer
fields tt_effdate           as   date 
fields tt_part                as character
fields tt_ponbr                as character
fields tt_poline                as integer
fields tt_receiver               as character
fields tt_rcvd_qty               as decimal
fields tt_rcvd_price               as decimal
fields tt_rcvd_amount               as decimal
fields tt_rcvd_totamount               as decimal

index idx1 tt_supplier tt_year tt_period 
.

define temp-table tt1_output like tt_output.
define temp-table tt2_output like tt_output.


define temp-table datasourcereportsettings  no-undo
   field setting_name   as character  
   field setting_value  as character.
   
   
      define variable queryString as character no-undo.
      
      
define temp-table tt_inputpar no-undo
     field tt_par like ps_par.      
      
      
      
      

define dataset dsReportResults for tt_output, ttSearchCondition , datasourcereportsettings.


define variable vl_debug as logical no-undo.

/*============================================================================*/
/* *****************************  MAIN BLOCK  ******************************* */
/*============================================================================*/

vl_debug = yes.

{com/qad/shell/report/reporting.i}
if vl_debug then 
              output to /home/mfg/xxqrftest.log.          

empty temp-table ttSearchCondition no-error.


create DataSourceReportSettings.
 assign
 DataSourceReportSettings.setting_name = "sys_search_criteria_display"
 DataSourceReportSettings.setting_value = "3".

 /*
create DataSourceReportSettings.
 assign
 DataSourceReportSettings.setting_name = "sys_render_as"
 DataSourceReportSettings.setting_value = "2".
 */
 
for first ttreportrequest no-lock:
   vhds = dataset dsreportresults:handle.
   run fillmetadata in reporthandle(vhds, "xxporcamountrp.meta.xml").
   
   run setreportmetadata (input reporthandle).

   if runreport then do:
      run runreport
     (output dataset-handle phreportresults).
   end.
   delete object phreportresults no-error.

end.                                                                                                

procedure setreportmetadata:
   define input parameter  ipreporthandle as handle.
   
   run overridemetadata.
end procedure. /* procedure modifymetadata */

procedure overridemetadata:

          
end procedure.

/* data retrieving logic block */
procedure runreport:

   define output parameter dataset-handle phreportresults.
   empty temp-table tt_output no-error. 
   empty temp-table tt1_output no-error. 
    

 
   for each ttFilterCondition no-lock
      where ttFilterCondition.BufferName = "ttSearchCondition":
  
    if ttFilterCondition.filterOperator = "="
    then do:
      
         if ttFilterCondition.bufferField = "tt_site" then
         assign vc_site1 = filtervalue 
                vc_site2 = filtervalue.
         if ttFilterCondition.bufferField = "tt_supplier" then
         assign vc_supplier1 = filtervalue 
                vc_supplier2 = filtervalue.
         if ttFilterCondition.bufferField = "tt_year" then
         assign vc_year = int(filtervalue).
               
         if ttFilterCondition.bufferField = "tt_period" then
         assign vc_period1 = int(filtervalue) 
                vc_period2 = int(filtervalue) .                
      
    end.
    
      
    if ttFilterCondition.filterOperator = ">="
    then do:
      
         if ttFilterCondition.bufferField = "tt_site" then
         assign vc_site1 = filtervalue .
         if ttFilterCondition.bufferField = "tt_supplier" then
         assign vc_supplier1 = filtervalue .
         if ttFilterCondition.bufferField = "tt_period" then
         assign vc_period1 = int(filtervalue). 
      
    end.
    
        if ttFilterCondition.filterOperator = "<="
    then do:
      
         if ttFilterCondition.bufferField = "tt_site" then
         assign vc_site2 = filtervalue .
         if ttFilterCondition.bufferField = "tt_supplier" then
         assign vc_supplier2 = filtervalue .
         if ttFilterCondition.bufferField = "tt_period" then
         assign vc_period2 = int(filtervalue). 
      
    end.
    
  end. /*for each ttFilterCondition no-lock*/
  
  if vc_supplier2 = "" then vc_supplier2 = hi_char.
  if vc_site2 = "" then vc_site2 = hi_char.
  if vc_period2 = 0 then vc_period2 = 12.
  
  
  find glc_cal where glc_domain = global_domain
                  and glc_year   = int(vc_year)
				  and glc_per    = int(vc_period1)
				  no-lock no-error.
	if available glc_cal then
    lc_dt1 = glc_start.
   find glc_cal where glc_domain = global_domain
                  and glc_year   = int(vc_year)
				  and glc_per    = int(vc_period2)
				  no-lock no-error.	
	if available glc_cal then
    lc_dt2 = glc_end.
	
   
   
   
   
   
   for each tr_hist no-lock where tr_domain = global_domain
                              and tr_effdate >= lc_dt1
                              and tr_effdate <= lc_dt2
                              and tr_site >= vc_site1
                              and tr_site <= vc_site2
                              and tr_addr >= vc_supplier1
                              and tr_addr <= vc_supplier2
                              and (tr_type = "rct-po" or tr_type = "iss-prv")
                              .
   find ad_mstr where ad_domain = global_domain
                  and ad_addr = tr_addr
                  no-lock no-error.
                  
   find glc_cal where glc_domain = global_domain
                  and glc_year   = year(tr_effdate)
				      and glc_start  <= tr_effdate
				      and glc_end    >= tr_effdate
				      no-lock no-error.               
                  
                  
   create tt_output.
   assign
          tt_output.tt_supplier       = tr_addr   
          tt_output.tt_supplierdesc   = ad_name   
          tt_output.tt_year           = year(tr_effdate)
          tt_output.tt_period         = glc_per
          tt_output.tt_effdate        = tr_effdate
          tt_output.tt_part           = tr_part
          tt_output.tt_ponbr          = tr_nbr
          tt_output.tt_poline         = tr_line
          tt_output.tt_receiver       = tr_lot
          tt_output.tt_rcvd_qty       = tr_qty_loc
          tt_output.tt_rcvd_price     = tr_price
          tt_output.tt_rcvd_amount    = tr_price * tr_qty_loc
          .
   
   
   
   end.   
   

  for each tt_output break by tt_output.tt_supplier:
  
    for each tt1_output where tt1_output.tt_supplier = tt_output.tt_supplier .
    
     tt_output.tt_rcvd_totamount = tt_output.tt_rcvd_totamount + tt1_output.tt_rcvd_amount.
     
    end.
  end.
   

 
   
  
   phreportresults = dataset dsreportresults:handle.
   delete object phreportresults no-error.

    
end.  /* run report */

